package com.chushouya.order.dao.repository;

import com.general.framework.core.enums.DelFlagEnum;
import com.general.framework.core.lang.Lists;
import com.general.framework.core.lang.Strings;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.order.dao.entity.OrderClothesProductEntity;
import com.chushouya.order.dao.mapper.OrderClothesProductEntityMapper;
import com.chushouya.order.dto.admin.orderclothesproduct.OrderClothesProductQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Repository
public class OrderClothesProductRepository extends BaseRepository<OrderClothesProductEntityMapper, OrderClothesProductEntity, Long> {

    /**
     * 根据订单ID查询旧衣商品列表
     */
    public OrderClothesProductEntity selectByOrderId(Long orderId) {
        if (orderId == null) {
            return null;
        }
        Example example = buildExample();
        example.and().andEqualTo("orderId", orderId);
        example.and().andEqualTo("delFlag", 0);
        return this.selectFirstOneByExample(example);
    }

    /**
     * 根据订单ID列表查询旧衣商品列表
     */
    public List<OrderClothesProductEntity> selectByOrderIds(List<Long> orderIds) {
        if (Lists.isEmpty(orderIds)) {
            return Lists.emptyList();
        }
        Example example = buildExample();
        example.and().andIn("orderId", orderIds);
        example.and().andEqualTo("delFlag", 0);
        return this.selectByExample(example);
    }

    /**
     * 根据地区编码查询旧衣商品列表
     */
    public List<OrderClothesProductEntity> selectByDistrictCode(String districtCode) {
        if (Strings.isBlank(districtCode)) {
            return Lists.emptyList();
        }
        Example example = buildExample();
        example.and().andEqualTo("districtCode", districtCode);
        example.and().andEqualTo("delFlag", 0);
        return this.selectByExample(example);
    }

    /**
     * 根据重量范围查询旧衣商品列表
     */
    public List<OrderClothesProductEntity> selectByWeightRange(String weightRange) {
        if (Strings.isBlank(weightRange)) {
            return Lists.emptyList();
        }
        Example example = buildExample();
        example.and().andEqualTo("weightRange", weightRange);
        example.and().andEqualTo("delFlag", 0);
        return this.selectByExample(example);
    }

    /**
     * 根据状态查询旧衣商品列表
     */
    public List<OrderClothesProductEntity> selectByStatus(Integer status) {
        if (status == null) {
            return Lists.emptyList();
        }
        Example example = buildExample();
        example.and().andEqualTo("status", status);
        example.and().andEqualTo("delFlag", 0);
        return this.selectByExample(example);
    }

    /**
     * 根据查询条件查询单个记录
     */
    public OrderClothesProductEntity selectOne(OrderClothesProductQuery query) {
        return this.selectFirstOneByExample(buildExample(query));
    }

    /**
     * 根据查询条件查询列表
     */
    public List<OrderClothesProductEntity> selectList(OrderClothesProductQuery query) {
        return this.selectByExample(buildExample(query));
    }

    /**
     * 构建查询条件
     */
    private Example buildExample(OrderClothesProductQuery query) {
        Example example = this.buildExample();
        Example.Criteria criteria = example.createCriteria();
        
        if (Objects.nonNull(query.getOrderClothesProductId())) {
            criteria.andEqualTo("orderClothesProductId", query.getOrderClothesProductId());
        }
        if (Objects.nonNull(query.getOrderId())) {
            criteria.andEqualTo("orderId", query.getOrderId());
        }
        if (Strings.isNotEmpty(query.getDistrictCode())) {
            criteria.andEqualTo("districtCode", query.getDistrictCode());
        }
        if (Strings.isNotEmpty(query.getDistrict())) {
            criteria.andLike("district", Strings.format("%{}%", query.getDistrict()));
        }
        if (Strings.isNotEmpty(query.getProductName())) {
            criteria.andLike("productName", Strings.format("%{}%", query.getProductName()));
        }
        if (Strings.isNotEmpty(query.getWeightRange())) {
            criteria.andEqualTo("weightRange", query.getWeightRange());
        }
        if (Objects.nonNull(query.getStatus())) {
            criteria.andEqualTo("status", query.getStatus());
        }
        if (Strings.isNotEmpty(query.getHandleUser())) {
            criteria.andLike("handleUser", Strings.format("%{}%", query.getHandleUser()));
        }
        if (Objects.nonNull(query.getCreateTimeStart()) && Objects.nonNull(query.getCreateTimeEnd())) {
            criteria.andBetween("createTime", query.getCreateTimeStart(), query.getCreateTimeEnd());
        }
        criteria.andEqualTo("delFlag", DelFlagEnum.normal.getValue());

        return example;
    }
}
